// pages/order/index.js
var app = getApp(),
  core = app.requirejs('core'),
  order = app.requirejs('biz/order');
var diyad = app.requirejs('biz/ad');
var util = require('../../utils/util.js')
const api = require("../../utils/api.js")
Page(app.sr.page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    catelists: [{
      id: 1,
      title: '全部订单',
      choseId: ''
    }, {
      id: 2,
      title: '待付款',
      choseId: 0
    }, {
      id: 3,
      title: '待收货',
      choseId: 2
    }, {
      id: 4,
      title: '待发货',
      choseId: 1
    }, {
      id: 5,
      title: '已签收',
      choseId: 3
    }, {
      id: 6,
      title: '已取消',
      choseId: 8
    }, ],
    current: 1, //页码
    status: '',
    list: [],
    loaded: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = 0
    let choseId = ''
    let navcontent = '全部订单'
    switch (options.status) {
      case '0':
        id = 2
        choseId = 0
        navcontent = '待付款'
        break
      case '1':
        id = 4
        choseId = 1
        navcontent = '待发货'
        break
      case '2':
        id = 3
        choseId = 2
        navcontent = '待收货'
        break
      case '3':
        id = 5
        choseId = 3
        navcontent = '已签收'
        break
      default:
        id = 1
        choseId = ''
        navcontent = '全部订单'
        break;
    }
    this.setData({
      options: options,
      status: choseId,
      active: id,
      navcontent,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      current: 1,
      list: [],
    })
    // console.log(111)
    this.get_list();
    /**启动广告 */
    diyad.get(this, function (res) {


    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取列表
  get_list: function () {
    console.log(111122)
    var $this = this;
    let apicontent = api.orders.getList()
    let list = $this.data.list
    let current = $this.data.current
    core.get(apicontent, {
      current,
      status: $this.data.status
    }, function (res) {
      var data = res.data
      if (res.code == 1) {
        $this.setData({
          show: false,
          total: data.total,
          empty: true,
          current: current + 1,
        });
        if (list.length > 0) {
          $this.setData({
            list: list.concat(data.records)
          });
        } else {
          $this.setData({
            list: data.records
          })
        }

        if (data.records.length < data.size || $this.data.list.length == data.total) {
          $this.setData({
            loaded: true
          });
        }
      } else {
        core.toast(list.message, 'loading')
      }
    });
  },
  // 切换导航
  tabNav(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    let status = e.currentTarget.dataset.type
    this.setData({
      status,
      list: [],
      active: id,
      current: 1
    })
    this.get_list()
  },
  //支付订单
  payOrder: function (e) {

    var list = this.data.list;
    var type = core.pdata(e).type;
    var id = e.target.dataset.id;
    var did = e.target.dataset.did;
    var $this = this;

    let apicontent = api.orders.prepay()
    let openid = app.getCache("uderInfo").openid
    core.post(apicontent, {
      orderId: id,
      shopId: 1,
      openid,
      rePay: 1,
    }, function (res) {
      if (res.code == 1) {
        core.pay(res.data, function (res) {
          if (res.errMsg == "requestPayment:ok") {
            $thids.get_list()
          }
        });
      } else {
        core.alert(res.message)
      }
    })
    // $this.get_updata_list(); //刷新页面
    var detail = list[did];
    var payinfo = list[did].payinfo.payinfo;
  },
  // 删除订单 未对接
  delete: function (e) {
    let _this = this
    var type = core.data(e).type,
      orderid = core.data(e).orderid;
    core.isToken(function (istoken) {
      let tokenData = istoken.data.token
      if (tokenData == 1) {
        order.delete(orderid, type, '/pages/order/index', _this);
      } else {
        core.getToken(function (getToken) {
          order.delete(orderid, type, '/pages/order/index', _this);
        })
      }
    })
  },
  // 查看物流信息
  checkexpress: function (e) {
    let expressid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/order/express/index?id=' + expressid,
    })
  },
}))